<template>
	<view>
		<u-picker :show="show" :columns="columns" keyName='faultName' @confirm="confirm" :title="pickerTitle"
			@cancel="cancelPicker" @change="changeHandler" :loading="loading">
		</u-picker>
		<u-datetime-picker :show="showDateTime" v-model="dateTime" mode="datetime" closeOnClickOverlay
			@confirm="confirmDateTime" :loading="loading" @cancel="cancelDateTime" :title="pickerDateTitle">
		</u-datetime-picker>
		<view class="content">
			<u--form labelPosition="left" :model="safeForm">
				<view class="add-zone">
					<view class="click-zone">
						<image src="/static/home_icons/delete.png" class="pix"></image>
						<image src="/static/home_icons/add.png" class="pix"></image>
					</view>
					<view class="add-form">
						<u-cell-group :border="false">
							<u-cell size="normal" title="问题类别" isLink :value="safeForm.faultName"
								@click="showPicker(0)">
							</u-cell>
							<view size="normal">
								<checkbox-group @change="checkboxChange" class="checkbox-zone">
									<checkbox :customStyle="{marginBottom: '16px'}" v-for="(box, index) in checkboxList"
										:key="index" :value="box.faultName" class="checkbox-item" size='30' color="red"
										@click="checkboxClick()" :checked="box.checked">
										{{box.faultName}}
									</checkbox>
								</checkbox-group>
							</view>
							<u-cell size="normal" title="问题部位">
								<view slot="value">
									<input type="text" style="text-align: right;" v-model="safeForm.issueLocation"
										placeholder="请输入" />
								</view>
							</u-cell>
							<u-cell size="normal" title="问题处理人">
								<view slot="value">
									<input type="text" style="text-align: right;" v-model="safeForm.handlerUserId"
										placeholder="请输入" />
								</view>
							</u-cell>
							<u-cell size="normal" title="发现时间" :value="safeForm.discoveryTime"></u-cell>
							<u-cell size="normal" title="预计处理时间" :value="safeForm.estimatedProcessingTime" isLink
								@click="showPicker(1)">
							</u-cell>
							<u-cell size="normal" title="问题描述" :border="false" :value="safeForm.input_num +'/200'">
							</u-cell>
							<view class="pixs">
								<textarea maxlength="200" @input="monitorInput(safeForm)"
									v-model="safeForm.issuesDesc" placeholder="请描述问题，最多输入200个字" />
							</view>
							<u-cell size="normal" title="现场情况" :border="false"></u-cell>
							<view class="pixs">
								<u-upload :fileList="fileList" @afterRead="afterRead" name="pix" :maxCount="6"
									width="188rpx" height="188rpx" :previewFullImage="true" multiple
									@delete="deletePic">
								</u-upload>
							</view>
						</u-cell-group>
					</view>
				</view>
			</u--form>
		</view>
		<view class="btns">
			<button type="default" class="next" @click="goto()">提交上传</button>
		</view>
	</view>
</template>

<script>
	import _utils from "@/common/_utils.js"
	import $C from '@/common/config.js'
	import _public from "@/common/public.js"
	import _file from "@/common/file.js"
	import _question from "@/common/question.js"

	export default {
		onLoad(_node) {
			this.node = JSON.parse(decodeURIComponent(_node.item))
			this.title = this.node.title
			console.log(this.node)
			this.safeForm = this.node
			this.safeForm.input_num = this.safeForm.issuesDesc.length
			this.getProgressList()
			this.init()
			this.initCheckBox()
			//照片回显
			if (this.node.issueImages != null) {
				let imgIdArray = this.node.issueImages.split(',')
				// this.fileList
				let temp = []
				for (let i = 0; i < imgIdArray.length; i++) {
					let element = {
						status: 'success',
						message: '',
						urlId: imgIdArray[i]
					}
					element.url = $C.baseUrl + '/file/download?fileId=' + imgIdArray[i]

					temp.push(element)
				}
				this.fileList = temp
			}
		},
		data() {
			return {
				//横向两端排列形式
				checkboxList: [],
				// u-checkbox-group的v-model绑定的值如果设置为某个checkbox的name，就会被默认选中
				checkboxValue: [],
				loading: false,
				pickerDateTitle: '',
				showDateTime: false,
				dateTime: Number(new Date()),
				safeForm: {},
				progressData: [],
				fileIds: [],
				flag: 0,
				formList: [],
				fileList: [],
				columns: [],
				columnData: [],
				pickerTitle: '',
				showEmpty: true,
				title: "",
				show: false,
				temp: 0,
			};
		},
		created() {

		},
		methods: {
			monitorInput(item) {
				this.safeForm.input_num = item.issuesDesc.length
			},
			async initCheckBox() {
				let result = await _public.getchild(this.node.faultId)
				let initbox = uni.$u.deepClone(result)
				if (this.node.issueTags != null) {
					let tags = this.node.issueTags.split(',')
					console.log(tags)
					for (let i = 0; i < initbox.length; i++) {
						if (tags.includes(initbox[i].faultName)) {
							initbox[i].checked = "true"
						}
					}
				}
				this.checkboxList = initbox
				console.log(this.checkboxList)
			},
			checkboxClick() {
				this.checkboxChange('item')
			},
			checkboxChange: function(e) {
				console.log(e)
				if (typeof e.type != 'undefined' && e.type == 'change') {
					this.checkboxValue = e.detail.value
				} else {
					this.safeForm.issueTags = this.checkboxValue.toString()
				}
			},
			async init() {
				let _i = await _public.getSafeData()
				// let all = _public.getChildData()
				let _j = await _public.getChildData()
				let _k = []
				_k.push(_i)
				_k.push(_j[0])
				this.columns = _k
				this.columnData = _j
			},
			goto() {
				//数据校验
				let flag = true
				let _form_data = this.safeForm
				if (_form_data.faultId == '') {
					uni.$showMsg('尚未选择问题类别')
					flag = false
				} else if (_form_data.issueLocation == '') {
					uni.$showMsg('尚未输入问题部位')
					flag = false
				} else if (_form_data.estimatedProcessingTime == '') {
					uni.$showMsg('尚未上选择预处理时间')
					flag = false
				} else if (_form_data.handlerUserId == '') {
					uni.$showMsg('尚未上输入问题处理人')
					flag = false
				} else if (_form_data.issuesDesc == '') {
					uni.$showMsg('尚未描述问题')
					flag = false
				} else if (this.fileList.length == 0) {
					uni.$showMsg('尚未上传现场图片')
					flag = false
				}
				//校验数据
				if (flag) {
					let cloneData = uni.$u.deepClone(this.safeForm)
					let pixs = []
					for (let j = 0; j < this.fileList.length; j++) {
						pixs.push(this.fileList[j].urlId)
					}
					cloneData.issuesStatus = 2
					cloneData.issueImages = pixs.toString()
					if (_question.modQuestionStatus(cloneData)) {
						uni.$showMsg("修改成功")
					} else {
						uni.$showMsg('提交失败')
					}
				}
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await _file.uploadFilePromise(lists[i].url)
					const temp = JSON.parse(result)
					console.log(temp)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						urlId: temp.body.fileId
					}))
					fileListLen++
				}
			},
			confirmDateTime(time) {
				this.safeForm.estimatedProcessingTime = uni.$u.timeFormat(time.value,
					'yyyy-mm-dd hh:MM')
				this.showDateTime = false
			},
			cancelPicker() {
				this.show = false
			},
			cancelDateTime() {
				this.showDateTime = false
			},
			changeHandler(e) {
				const {
					columnIndex,
					index,
					picker
				} = e
				if (columnIndex === 0) {
					this.loading = true
					// 模拟网络请求
					uni.$u.sleep(500).then(() => {
						picker.setColumnValues(1, this.columnData[index])
						this.loading = false
					})
				}
			},
			//待测试
			async confirm(item) {
				console.log(item)
				switch (this.flag) {
					case 0:
						if (typeof item.value[1] == undefined) {
							this.safeForm.faultName = item.value[0].faultName
							this.safeForm.faultId = item.value[0].faultId
						} else {
							this.safeForm.faultName = item.value[0].faultName + '-' + item.value[1]
								.faultName
							// if()
							this.safeForm.faultId = item.value[1].faultId
							this.checkboxList = await _public.getchild(item.value[1].faultId)
						}

						break;
				}
				this.show = false
			},
			//获取进度列表
			async getProgressList() {
				const {
					data
				} = await uni.$http.get(uni.$http.baseURL + '/constant/getByType', {
					type: 1
				})
				if (data.message == '成功') {
					let list = data.body
					for (let i = 0; i < list.length; i++) {
						this.progressData.push(list[i].constantValue)
					}
				}
			},
			showPicker(item) {
				this.flag = item
				switch (this.flag) {
					case 0:
						this.pickerTitle = '安全问题类别'
						// this.columns = [this.progressData]
						this.show = true
						break;
					case 1:
						this.pickerDateTitle = '预计处理时间'
						this.showDateTime = true
						break;
				}
			}

		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: block;
		padding: 20rpx 30rpx 58rpx 30rpx;

		.process-bar {
			display: block;
			border: 1rpx solid #efefef;
			border-radius: 20rpx;
			background-color: #FFFFFF;
			padding: 30rpx;
			margin-bottom: 20rpx;

			.steps {
				margin-top: 45rpx;
			}
		}

		.safeForm {
			display: block;

			.add-zone {
				width: 100%;
			}
		}
	}

	.add-zone {
		padding: 20rpx;
		border: 1rpx solid #efefef;
		border-radius: 20rpx;
		background-color: #FFFFFF;
	}

	.add-form {
		display: flex;
		flex-direction: column;

		.pixs {
			padding: 20rpx;

		}
	}

	.click-zone {
		display: flex;
		flex-direction: row;
		justify-content: space-between;

		.pix {
			width: 40rpx;
			height: 40rpx;
		}
	}

	.btns {
		margin-top: 88rpx;
		display: flex;
		flex-direction: row;
		margin-bottom: 95rpx;

		.next {
			width: 560rpx;
			color: #FFFFFF;
			background: linear-gradient(90deg, #E62129 0%, #BA000A 100%);
			box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
			border-radius: 44rpx;
		}
	}

	textarea {
		// width: 630rpx;
		height: 160rpx;
		background: #F5F5F5;
		border-radius: 10rpx;
	}

	.numLimit {
		position: relative;
		top: -40rpx;
		right: -530rpx;

	}

	.checkbox-zone {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		padding: 20rpx 30rpx;
		// text-align: center;

		.checkbox-item {
			margin-top: 10rpx;
			width: 50%;
		}
	}
</style>
